<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <style>
    * {
      margin: 0;
      padding: 0;
    }

    ul,
    li {
      list-style: none;
    }

    ul {
      height: 40px;
      margin: 30px auto;
      border-bottom: 3px solid pink;
      width: 1000px;
      position: relative;
    }

    li {
      height: 100%;
      width: 100px;
      float: left;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
    }

    ul>span {
      width: 100px;
      height: 3px;
      position: absolute;
      left: 0;
      bottom: -3px;
      background-color: red;
    }

    ul>p {
      width: 100px;
      height: 40px;
      background-color: skyblue;
      color: #fff;
      position: absolute;
      top: 0;
      left: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      cursor: pointer;
      border-bottom: 2px solid red;
    }
  </style>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>

<body>
  <ul>
    <li>第1个选项</li>
    <li>第2个选项</li>
    <li>第3个选项</li>
    <li>第4个选项</li>
    <li>第5个选项</li>
    <li>第6个选项</li>
    <li>第7个选项</li>
    <li>第8个选项</li>
    <li>第9个选项</li>
    <li>第10个选项</li>
    <p>第1个选项</p>
  </ul>
</body>

</html>
<script src='https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'></script>
<script>

  //声明一个开关,默认是关闭的

  let flag = false;

  // 1.p标签跟随鼠标滑动

  $('ul').on('mouseenter', 'li', function () {
    flag = false;
    // console.log(this)
    $('p').stop().animate({ left: $(this).position().left }, 200).text($(this).text())
  })

  //2.鼠标离开 p标签回到原点

  $('ul').mouseleave(function () {
    if (flag) { //如果开关打开了 就不回到原点
      return
    }
    $('p').animate({ left: 0 }).text('第1个选项')
  })

  //3.给p标签注册点击事件

  $('p').click(function(){
    flag = true;
  })

</script>